<?php
/**
 * @package scuttle
 * @file /actions/BookmarksEditAction.class.php
 **/

if (!defined('XOOPS_ROOT_PATH')) exit();

require_once XOOPS_MODULE_PATH . '/scuttle/class/AbstractEditAction.class.php';
require_once XOOPS_MODULE_PATH . '/scuttle/forms/BookmarksEditForm.class.php';
require_once XOOPS_MODULE_PATH. '/scuttle/include/TagService.php';

class Scuttle_BookmarksEditAction extends Scuttle_AbstractEditAction
{

	var $tagCloudArray = array();
	var $entoryTags = null;
	var $bId= null;
	var $moduleHeader = null;
	var $ret= false;

	function _getId()
	{
		return xoops_getrequest('bId');
	}

	function _getUrl()
	{
		return xoops_getrequest('url');
	}

	function _getTitle()
	{
		return xoops_getrequest('bTitle');
	}

	function _getTags()
	{
		return xoops_getrequest('tags');
	}

	function _getImgDel()
	{
		return xoops_getrequest('imgdel');
	}

	function _getRet()
	{
		return xoops_getrequest('ret');
	}

	function _getDelete()
	{
		return xoops_getrequest('_form_control_delete');
	}

	function &_getHandler()
	{
		$handler =& xoops_getmodulehandler('bookmarks');
		return $handler;
	}

	function _setupActionForm()
	{
		$this->mActionForm =& new Scuttle_BookmarksEditForm();
		$this->mActionForm->prepare();
	}

	function _setupObject()
	{
		$handler =& $this->_getHandler();
		if ($this->_getUrl() != null) {
			// edit to book marklet
			$mCriteria =& new CriteriaCompo();
			$mCriteria->add(new Criteria('bAddress', $this->_getUrl()));
			$mCriteria->setLimit(1);
			$this->mObjects =& $handler->getObjects($mCriteria);
			if (count($this->mObjects)>0) {
				foreach($this->mObjects as $mObject) {
					$this->mObject = $mObject;
				}
				$this->bId = $this->mObject->getShow('bId');
			}
		} else {
			// new or edit
			$id = $this->_getId();
			$this->mObjectHandler = $this->_getHandler();
			$this->mObject =& $handler->get($id);
		}

		$this->ret = xoops_getrequest('ret');

		if ($this->mObject == null && $this->isEnableCreate()) {
			$this->mObject =& $handler->create();
		}
	}


	function hasPermission(&$controller, &$xoopsUser, &$moduleConfig)
	{
		if ($controller->mRoot->mContext->mUser->isInRole('Module.scuttle.Admin')) {
			return true;
		}
		if (is_object($xoopsUser)) {
			$uid = $this->mObject->getShow('uId');
			if ($uid > 0 && $uid != $xoopsUser->uid()) {
				$controller->executeRedirect('index.php', 1, _MD_SC_ERROR_EDIT_FOR_PERMISSION);
			}
		} else {
			// user login
			$xoops_redirect = XOOPS_MODULE_URL .'/scuttle/index.php?action=BookmarksEdit&ret=1&'.$this->_getUrl() ;
			$xoops_redirect = urldecode($xoops_redirect);
			$url = XOOPS_URL. '/user.php';
			$controller->executeRedirect($url, 1, _MD_SC_ERROR_EDIT_FOR_PERMISSION);
		}
		return true;
	}

	function execute(&$controller, &$xoopsUser)
	{
		$ret = parent::execute($controller, $xoopsUser);
		if ($ret == SC_FRAME_VIEW_SUCCESS) {
			// save tags
			if ($this->mObject->isNew()) {
				// is new item
				$root =& XCube_Root::getSingleton();
				$db =& $root->mController->mDB;
				$bookmarkId = $db->getInsertId();
			} else {
				$bookmarkId = $this->mObject->getShow('bId');
			}
			if (!$this->_getDelete()) {
				$getTags = htmlSpecialChars($this->_getTags(), ENT_QUOTES ) ;
				// save tags
				$tagHandler = xoops_getmodulehandler('tags');
				$tagHandler->saveTags($bookmarkId, $getTags);
			}
		}
		return $ret;
	}

	function getDefaultView(&$controller, &$xoopsUser)
	{
		if ($this->mObject == null) {
			return SC_FRAME_VIEW_ERROR;
		}
		if (is_null($this->bId)) {
			$this->bId = $this->_getId();
		}

		if ($this->_getUrl() != null & $this->_getId() == 0) {
			$this->mObject->set('bAddress', $this->_getUrl());
		}

		if ($this->_getTitle() != null & $this->mObject->getShow('bTitle') == null) {
			$this->mObject->set('bTitle', $this->_getTitle());
		}

		// tag to text
		$tagHandler = xoops_getmodulehandler('tags');
		$this->entoryTags = $tagHandler->getTagList($this->bId);

		return SC_FRAME_VIEW_INPUT;
	}


	function executeViewInput(&$controller, &$xoopsUser, &$render)
	{
		$this->mActionForm->load($this->mObject);

		// Popular Tag Cloud
		$popularTags =TagService::getPopularTags($limit = 150, $days = NULL) ;
		if (count($popularTags) > 0){
			$this->tagCloudArray = TagService::tagCloud($popularTags, $steps = 20, $sizemin = 120, $sizemax = 300);
		}

		$root =& XCube_Root::getSingleton();
		$this->breadcrumbs[] = array('name' => $root->mContext->mModule->mXoopsModule->getVar('name') , 'url' => XOOPS_MODULE_URL . '/scuttle/' ) ;
		$this->breadcrumbs[] = array('name' => _MD_SC_BOOKMARKS_EDIT) ;

		$render->setTemplateName('scuttle_bookmarks_edit.html');
		$render->setAttribute('actionForm' , $this->mActionForm);
		$render->setAttribute('object'     , $this->mObject);
		$render->setAttribute('config'     , $this->mConfig);
		$render->setAttribute('ret', $this->ret);
		$render->setAttribute('entory_tags'     , $this->entoryTags);

		// set select box array to html_options
		$render->setAttribute('rangeOptions', array("0" => _MD_SC_BOOKMARKS_RANGE_0, "2" => _MD_SC_BOOKMARKS_RANGE_1, "1" => _MD_SC_BOOKMARKS_RANGE_2));
		$render->setAttribute('typeOptions', array("0" => _MD_SC_BOOKMARKS_SHARE_0, "1" => _MD_SC_BOOKMARKS_SHARE_1, "2" => _MD_SC_BOOKMARKS_SHARE_2));
		$render->setAttribute('tag_cloud' , $this->tagCloudArray);
		$render->setAttribute('xoops_breadcrumbs'     , $this->breadcrumbs) ;

		$this->moduleHeader = '<link rel="stylesheet" type="text/css" href="' . XOOPS_MODULE_URL. '/scuttle/module.css" />'."\n";
		$this->moduleHeader.= '<script type="text/javascript" src="' . XOOPS_MODULE_URL. '/scuttle/js/edittag.js"></script>'."\n";
		$render->setAttribute('xoops_module_header',$this->moduleHeader);
	}

	function _doExecute()
	{
		// save path check
		$savePath = XOOPS_UPLOAD_PATH . "/scuttle/";
		if (!file_exists($savePath)) {
			return SC_FRAME_VIEW_ERROR;
		}
		if ($this->_getDelete()) {
			// delete data
			// delete tags
			$tagHandler = xoops_getmodulehandler('tags');
			$tagHandler->deleteTagId($this->mObject->getShow('bId'));
			// delete bookmark
			$handler =& $this->_getHandler();
			if ($handler->delete($this->mObject)) {
				// delete image
				$filename = XOOPS_UPLOAD_PATH . "/scuttle/" . $this->mObject->getShow('image') ;
				if (file_exists($filename)) {
					unlink($filename);
				}
				return SC_FRAME_VIEW_SUCCESS;
			} else {
				return SC_FRAME_VIEW_ERROR;
			}
		} elseif ($this->_getImgDel() ==1) {
			// delete chack box
			$filename = XOOPS_UPLOAD_PATH . "/scuttle/" . $this->mActionForm->mOldFileName ;
			if (file_exists($filename)) {
				unlink($filename);
			}
			// over ride to set object 'image' is null
			$this->mObject->set('image' , "");
		} else {
			// save image
			if ($this->mActionForm->mFormFile != null) {
				if (!$this->mActionForm->mFormFile->saveAs(XOOPS_UPLOAD_PATH.'/scuttle')) {
					return false;
				}
			}
			// delete image
			if ($this->mActionForm->mOldFileName != null and $this->mActionForm->mFormFile != null) {
				$filename = XOOPS_UPLOAD_PATH . "/scuttle/" . $this->mActionForm->mOldFileName ;
				if (file_exists($filename)) {
					unlink($filename);
				}
			}
		}
		// save object to SQL
		return parent::_doExecute();
	}

	function executeViewSuccess(&$controller, &$xoopsUser, &$render)
	{
		if($this->ret == 1){
			// if using bookmarklet to return  bookmarks url
			$controller->executeForward($this->mObject->get('bAddress'));
		} else {
			$controller->executeRedirect('index.php', 1, _MD_SC_BOOKMARKS_EDIT_SUCCESS);
		}
	}

	function executeViewError(&$controller, &$xoopsUser, &$render)
	{
		$url = 'index.php';
		$controller->executeRedirect($url, 1, _MD_SC_BOOKMARKS_ERROR_EDIT);
	}

	function executeViewCancel(&$controller, &$xoopsUser, &$render)
	{
		$url = 'index.php';
		$controller->executeForward($url);
	}

}
?>